<template>
  <div class="MyArticle">
    <el-breadcrumb separator-class="el-icon-arrow-right">
      <el-breadcrumb-item :to="{ path: '/welcome' }">首页</el-breadcrumb-item>
      <el-breadcrumb-item>文章管理</el-breadcrumb-item>
      <el-breadcrumb-item>我的文章</el-breadcrumb-item>
    </el-breadcrumb>
    <el-card style="margin-top:15px;">
      <el-row>
        <el-col>
          <el-radio v-model="query" label="1" @change="tiaojian">全部</el-radio>
          <el-radio v-model="query" label="2" @change="tiaojian">草稿</el-radio>
          <el-radio v-model="query" label="3" @change="tiaojian">待审核</el-radio>
          <el-radio v-model="query" label="4" @change="tiaojian">已发布</el-radio>
        </el-col>
      </el-row>
      <el-table :data="myArticle" style="width: 100%">
        <el-table-column label="#" type="index"></el-table-column>
        <el-table-column label="创建时间" prop="addTime"></el-table-column>
        <el-table-column label="文章标题" prop="title"></el-table-column>
        <el-table-column label="文章分类" prop="category.name"></el-table-column>
        <el-table-column label="文章摘要" prop="description"></el-table-column>
        <el-table-column label="文章状态" prop="isCaogao">
          <template slot-scope="scope">
            <i v-if="scope.row.isCaogao==false" style="color:red;">草稿</i>
            <i v-else>
              <i v-if="scope.row.isStatus==false" style="color:green;">待审核</i>
              <i v-else style="color:blue;">已发布</i>
            </i>
          </template>
        </el-table-column>

        <el-table-column label="操作" width="270px">
          <template slot-scope="scope">
            <el-button size="mini" type="primary" @click="editArticle(scope.$index, scope.row)">编辑</el-button>
            <el-button
              size="mini"
              type="info"
              @click="fabu(scope.$index, scope.row)"
              v-if="scope.row.isCaogao==false"
            >发布</el-button>
            <el-button size="mini" type="danger" @click="handleDelete(scope.$index, scope.row)">删除</el-button>
          </template>
        </el-table-column>
      </el-table>
    </el-card>
    <!-- 编辑模态框 -->
    <el-dialog title="提示" :visible.sync="editVisbel" width="60%">
      <el-form :model="EarticleForm" status-icon ref="EarticleFormRef" label-width="100px">
        <el-form-item label="标题">
          <el-input v-model="EarticleForm.title" autocomplete="off"></el-input>
        </el-form-item>
        <el-form-item label="选择分类">
          <el-select v-model="EarticleForm.category" placeholder="请选择">
            <el-option
              v-for="item in cateList"
              :key="item._id"
              :label="item.name"
              :value="item._id"
            ></el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="文章摘要">
          <el-input v-model="EarticleForm.description" autocomplete="off"></el-input>
        </el-form-item>
        <el-form-item label="文章封面">
          <el-upload
            class="avatar-uploader"
            action="/api/upload/upload"
            :show-file-list="false"
            :on-success="seccessAfter"
            :before-upload="beforeAvatarUpload"
          >
            <img v-if="EarticleForm.artcleImg" :src="EarticleForm.artcleImg" class="avatar" />
            <i v-else class="el-icon-plus avatar-uploader-icon"></i>
          </el-upload>
        </el-form-item>
        <el-form-item label="文章内容">
          <quill-editor v-model="EarticleForm.content"></quill-editor>
        </el-form-item>
      </el-form>
      <span slot="footer" class="dialog-footer">
        <el-button @click="editVisbel = false">取 消</el-button>
        <el-button type="primary" @click="submitEdit">确 定</el-button>
      </span>
    </el-dialog>
  </div>
</template>
<script>
export default {
  name: 'MyArticle',
  data() {
    return {
      query: '1',
      myArticle: [],
      editVisbel: false,
      EarticleForm: {
        title: '',
        category: '',
        description: '',
        artcleImg: '',
        content: '',
        id: ''
      },

      cateList: []
    }
  },
  created() {
    this.mydata()
    this.getcategory()
  },
  methods: {
    mydata() {
      this.$axios
        .get('/api/admin/MyArticle', { params: { query: this.query } })
        .then(result => {
          this.myArticle = result.data
        })
    },
    getcategory() {
      this.$axios.get('/api/admin/category').then(result => {
        // console.log(result.data)
        this.cateList = result.data
      })
    },
    tiaojian() {
      this.mydata()
      console.log(this.query)
    },
    editArticle(index, row) {
      console.log(row, 12)
      this.editVisbel = true
      this.EarticleForm.title = row.title
      this.EarticleForm.category = row.category._id
      this.EarticleForm.description = row.description
      this.EarticleForm.artcleImg = row.artcleImg
      this.EarticleForm.content = row.content
      this.EarticleForm.id = row._id
    },
    fabu(index, row) {
      this.$confirm('确定发布该文章, 是否继续?', '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      })
        .then(() => {
          this.$axios.put(`/api/admin/fabu/${row._id}`).then(result => {
            this.$message({
              type: result.data.type,
              message: result.data.msg
            })
            this.mydata()
          })
        })
        .catch(() => {
          this.$message({
            type: 'info',
            message: '已取消删除'
          })
        })
    },
    handleDelete(index, row) {
      this.$confirm('确定删除该文章, 是否继续?', '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      })
        .then(() => {
          this.$axios.delete(`/api/admin/deletd/${row._id}`).then(result => {
            this.$message({
              type: result.data.type,
              message: result.data.msg
            })
            this.mydata()
          })
        })
        .catch(() => {
          this.$message({
            type: 'info',
            message: '已取消删除'
          })
        })
    },
    seccessAfter(response, file, fileList) {
      const a = response.path.split('\\')
      this.EarticleForm.artcleImg = 'http://localhost:5000/' + a[a.length - 1]
    },
    beforeAvatarUpload(file) {
      const isJPG = file.type === 'image/jpeg'
      const isLt2M = file.size / 1024 / 1024 < 2

      if (!isJPG) {
        this.$message.error('上传头像图片只能是 JPG 格式!')
      }
      if (!isLt2M) {
        this.$message.error('上传头像图片大小不能超过 2MB!')
      }
      return isJPG && isLt2M
    },
    submitEdit() {
      this.$refs.EarticleFormRef.validate(valid => {
        if (valid) {
          this.$axios
            .put(`/api/admin/edit/${this.EarticleForm.id}`, this.EarticleForm)
            .then(result => {
              this.$message({
                type: result.data.type,
                message: result.data.msg
              })
              this.editVisbel = false
              this.mydata()
            })
        } else {
          console.log('error submit!!')
          return false
        }
      })
    }
  }
}
</script>
<style scoped>
.avatar-uploader .el-upload {
  border: 1px dashed #d9d9d9;
  border-radius: 6px;
  cursor: pointer;
  position: relative;
  overflow: hidden;
}
.avatar-uploader .el-upload:hover {
  border-color: #409eff;
}
.avatar-uploader-icon {
  font-size: 28px;
  color: #8c939d;
  width: 178px;
  height: 178px;
  line-height: 178px;
  text-align: center;
  box-shadow: 0 0 10px #ccc;
}
.avatar {
  width: 178px;
  height: 178px;
  display: block;
}
</style>